import 'package:FlutterStudy/demo/MyAnimatedWidget.dart';
import 'package:flutter/material.dart';

class AnimationDemo extends StatefulWidget {
  @override
  _AnimationDemoState createState() {
    return _AnimationDemoState();
  }
}

class _AnimationDemoState extends State<AnimationDemo>
    with TickerProviderStateMixin {
  late AnimationController mController;

  @override
  void initState() {
    super.initState();
    mController = AnimationController(
      vsync: this,
      lowerBound: 1,
      upperBound: 200,
      duration: Duration(seconds: 5),
    );
    mController.addListener(() {
    /*  setState(() {

      });*/
      debugPrint(" 控制动画的值 ${mController.value}");
    });
    mController.addStatusListener((state){
      debugPrint(" 动画的状态值 ${state}");



    });
   // Tween(begin: 0,end: 200).animate(mController);
   // Tween(begin: 0,end: 200).animate(mController);


  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Stack(
      children: [
        Center(
          child: AnimatedHeartIcon(
            onPressed: (){
              switch(mController.status){

                case AnimationStatus.completed:
                  mController.reverse();
                  break;
                default:{
                  mController.forward();
                }
              }


            } ,
            // icon: Icon(Icons.heart_broken,),
            // iconSize: mController.value,
            animation: mController ,
          ),
        ),
        MyAnimatedWidget()
      ],
    );
  }
}

// 使用 AnimatedWidget 创建自定义动画组件
class AnimatedHeartIcon extends AnimatedWidget {
  final Animation<double> animation;
  final VoidCallback onPressed;

  AnimatedHeartIcon({
    Key? key,
    required this.animation,
    required this.onPressed,
  }) : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: onPressed,
      icon: Icon(Icons.heart_broken),
      iconSize: animation.value,
    );
  }
}
